package cn.lili.modules.goods.entity.dto;

import cn.lili.common.validation.EnumValue;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.goods.entity.enums.SalesModeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商品编辑DTO
 *
 * @author pikachu
 * @since 2020-02-24 19:27:20
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class GoodsOperationDTO implements Serializable {

    @Serial
    private static final long serialVersionUID = -509667581371776913L;

    @ApiModelProperty(value = "模版id")
    private String draftId;

    @ApiModelProperty(hidden = true)
    private String goodsId;

    @ApiModelProperty(hidden = true)
    private String scene;

    @ApiModelProperty(value = "商品价格", required = true)
    @NotNull(message = "商品价格不能为空")
    @Min(value = 0, message = "商品价格不能为负数")
    @Max(value = 99999999, message = "商品价格不能超过99999999")
    private Double price;

    /**
     * 会员价
     */
    private Double vipPrice;

    @ApiModelProperty(value = "分类path")
    private String categoryPath;

    @ApiModelProperty(value = "子分类路径，多个按-分割")
    private String categoryPathSon;

    @ApiModelProperty(value = "子分类名称，多个按-分割")
    private String categoryPathSonName;

    @ApiModelProperty(value = "店铺分类id", required = true)
    @Size(max = 200, message = "选择了太多店铺分类")
    private String storeCategoryPath;

    @ApiModelProperty(value = "品牌id")
    @Min(value = 0, message = "品牌值不正确")
    private String brandId;

    @ApiModelProperty(value = "商品名称", required = true)
    @NotEmpty(message = "商品名称不能为空")
    @Length(max = 255, message = "商品名称不能超过255个字符")
    private String goodsName;

    @ApiModelProperty(value = "详情")
    private String intro;

    @ApiModelProperty(value = "商品移动端详情")
    private String mobileIntro;

    @ApiModelProperty(value = "库存")
    @Min(value = 0, message = "库存不能为负数")
    @Max(value = 99999999, message = "库存不能超过99999999")
    private Integer quantity;

    @ApiModelProperty(value = "是否立即发布")
    private Boolean release;

    @ApiModelProperty(value = "是否是推荐商品")
    private Boolean recommend;

    @ApiModelProperty(value = "是否立即上架")
    private Boolean readyForSale = false;

    @ApiModelProperty(value = "商品参数")
    private List<GoodsParamsDTO> goodsParamsDTOList;

    @ApiModelProperty(value = "商品图片")
    private List<String> goodsGalleryList;

    @ApiModelProperty(value = "运费模板id,不需要运费模板时值是0", required = true)
    @NotNull(message = "运费模板不能为空，没有运费模板时，传值0")
    @Min(value = 0, message = "运费模板值不正确")
    private String templateId;

    @ApiModelProperty(value = "模版商品标识")
    private Boolean templateGoodsFlag = false;

    @ApiModelProperty(value = "sku列表")
    @Valid
    private List<Map<String, Object>> skuList;

    @ApiModelProperty(value = "卖点")
    private String sellingPoint;

    @ApiModelProperty(value = "销售模式", required = true)
    private String salesModel;

    @ApiModelProperty(value = "是否有规格", hidden = true)
    private String haveSpec;

    @ApiModelProperty(value = "商品单位", required = true)
    @NotEmpty(message = "商品单位不能为空")
    private String goodsUnit;

    @ApiModelProperty(value = "商品描述")
    private String info;

    @ApiModelProperty(value = "是否重新生成sku数据")
    private Boolean regeneratorSkuFlag = true;

    /**
     * @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
     */
    @ApiModelProperty(value = "商品类型")
    @EnumValue(strValues = {"PHYSICAL_GOODS", "VIRTUAL_GOODS", "E_COUPON","BLINDBOX_GOODS"}, message = "商品类型参数值错误")
    private String goodsType;

    /**
     * 商品视频
     */
    @ApiModelProperty(value = "商品视频")
    private String goodsVideo;

    @ApiModelProperty(value = "第三方id")
    private String thirdPartId;

//    @ApiModelProperty(value = "税率 例如：百分之18.5，则值为 18.5")
//    private Double taxRate;
//
//    @ApiModelProperty(value = "含税协议价")
//    private Double thirdPrice;
//
//    @ApiModelProperty(value = "未税协议价")
//    private Double nakedPrice;

    @ApiModelProperty(value = "卖家id")
    private String storeId;

    @ApiModelProperty(value = "店铺名称")
    private String storeName;

    @ApiModelProperty(value = "保留字段1", hidden = true)
    private String field1;

    @ApiModelProperty(value = "保留字段2", hidden = true)
    private String field2;

    @ApiModelProperty(value = "保留字段3", hidden = true)
    private String field3;

    @ApiModelProperty(value = "保留字段4", hidden = true)
    private String field4;

    @ApiModelProperty(value = "保留字段5", hidden = true)
    private String field5;

    @ApiModelProperty(value = "扩展字段，可自由存储，数据库为text格式", hidden = true)
    private String ext;

    @ApiModelProperty(value = "商品来源，1:手动，2：采集；3：api接口获取", required = true)
    private Integer  goodsFrom;

    @ApiModelProperty(value = "商品采集地址", required = true)
    private String goodsGrabUrl;

    @ApiModelProperty(value = "商品价值（盲盒商品专用）")
    private String goodsValue;

    @ApiModelProperty(value = "规则ID")
    private String ruleId;

    @ApiModelProperty(value = "是否为绿色商品，true/false,是/否")
    private Boolean isGreenGoods = false;

    @ApiModelProperty(value = "商品默认的展示价格")
    private BigDecimal defaultSkuGreenScore;

    @ApiModelProperty(value = "审核状态,默认是待审核")
    private String authFlag = GoodsAuthEnum.TOBEAUDITED.name();

    @ApiModelProperty(value = "审核状态,默认是下架")
    private String marketEnable = GoodsMarketEnum.DOWN.description();

    /**
     * 商品ids
     */
    private String goodsIds;

    /**
     * 分类ids
     */
    private String categoryPathIds;

    @ApiModelProperty(value = "是否公益商品，true/false,是/否")
    private Boolean isCommonwealGoods = false;

    @ApiModelProperty(value = "是否会员商品")
    private Boolean isVipGoods;

    @ApiModelProperty(value = "是否绿色积分卷抵扣")
    private Boolean isGreenSecurities;

    @ApiModelProperty(value = "规格参数")
    private String specParameter;

    @ApiModelProperty(value = "是否演唱会门票")
    private Boolean isTicket;

    @ApiModelProperty(value = "是否套餐商品")
    private Boolean isMeal;

    @ApiModelProperty(value = "是否是分销商品")
    private Boolean isDistributionGoods;

    @ApiModelProperty(value = "是否开启一级分销")
    private Boolean isLevelDistribution;

    @ApiModelProperty(value = "是否允许取消订单")
    private Boolean isCancel;

    @ApiModelProperty(value = "礼包Id")
    private String packId;

    @ApiModelProperty("免税归属id: goodsBelong主键id")
    private String goodsBelongId;

    /**
     * 是否免税商品
     */
    private Boolean isFree;

    @ApiModelProperty(value = "第三方供应链平台id")
    private String thirdPartyPlatformId;

    @ApiModelProperty(value = "第三方供应链类型（1壹邦玖，2企叮咚 4,免税）")
    private Integer thirdPartyPlatformType;


    public GoodsOperationDTO(GoodsImportDTO goodsImportDTO) {
        this.price = goodsImportDTO.getPrice();
        this.goodsName = goodsImportDTO.getGoodsName();
        this.intro = goodsImportDTO.getIntro();
        this.mobileIntro = goodsImportDTO.getIntro();
        this.quantity = goodsImportDTO.getQuantity();
        this.goodsGalleryList = goodsImportDTO.getGoodsGalleryList();
        this.templateId = goodsImportDTO.getTemplate();
        this.sellingPoint = goodsImportDTO.getSellingPoint();
        this.salesModel = goodsImportDTO.getSupportPurchase() ? SalesModeEnum.PURCHASING.name() : SalesModeEnum.RETAIL.name();
        this.goodsUnit = goodsImportDTO.getGoodsUnit();
        this.goodsType =goodsImportDTO.getGoodsType();
        this.release = goodsImportDTO.getRelease();
        this.recommend = false;
    }

    public String getGoodsName() {
        //对商品对名称做一个极限处理。这里没有用xss过滤是因为xss过滤为全局过滤，影响很大。
        // 业务中，全局代码中只有商品名称不能拥有英文逗号，是由于商品名称存在一个数据库联合查询，结果要根据逗号分组
        if(null==goodsName || "".equals(goodsName)){
            return goodsName;
        }
        return goodsName.replace(",", "");
    }

}
